<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/27
 * Time: 11:58
 */

namespace App\Http\Controllers;

use App\Department;
use App\DepartmentUser;
use App\Handbook;
use App\Type;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\NavigationList;
use App\NavigationType;



header('Access-Control-Allow-Origin:*');

class UserController extends Controller{

    //登录
    public function login(){
        return view('web.login');
    }

    public function loginIn(){

        if(!isset($_POST['username']) || !$_POST['username']){

            return $this->json(0,'用户名为空或者不存在！','');

        }elseif (!isset($_POST['password']) || !$_POST['password']){

            return $this->json(0,'密码为空或者不存在！','');
        }

        $res = DepartmentUser::where(['username'=>$_POST['username'],'password'=>md5(md5($_POST['password']))])->first(['id','username']);

        if($res){

            return $this->json(1,'登录成功！',$res);
        }else{
            return $this->json(0,'用户不存在！','');
        }

    }

    public function handbook(){
        $username = $_GET['username'];
        $type_id = $_GET['type_id'];

        $user_info = DepartmentUser::where('username',$username)->first(['id','depart_id']);

        if($user_info){

            if($user_info->depart_id == 0){//超级管理员

                $typelist = $this->getTypeList();

                foreach ($typelist as $k=>$v){
                    if(!$v['typelist']){
                        unset($typelist[$k]);
                    }
                }
                $booklist = $this->getBookList($type_id);

            }else{

                $typelist = $this->getTypeList($user_info->depart_id);

                if(!$type_id){

                    $type_ids = [];
                    foreach ($typelist as $k=>$v){
                        if(!$v['typelist']){
                            unset($typelist[$k]);
                        }else{
                            foreach ($v['typelist'] as $kk=>$vv){
                                array_push($type_ids,$vv['id']);
                            }
                        }
                    }

                    $booklist = $this->getBookList($type_ids);


                }else{
                    foreach ($typelist as $k=>$v){
                        if(!$v['typelist']){
                            unset($typelist[$k]);
                        }
                    }
                    $booklist = $this->getBookList($type_id);
                }
            }

        }else{
            $typelist = $handbook = [];
        }

        $booklist->username = $username;
        $booklist->type_id = $type_id;

        return view('web.hankbook',['list'=>$typelist,'handbook'=>$booklist]);
    }


    public function getTypeList($depart_id=0){

        $key = 'handbook_type_list_'.$depart_id;
        $type_list = Cache::get($key);

        if(!$type_list){
            if($depart_id){
                $type_list = Department::with([
                    'typelist' => function($query){
                        $query->select('id','type_name','depart_id')->orderBy('sort','desc');
                    }
                ])->where('id',$depart_id)
                    ->select('id','department_name')
                    ->orderBy('id','desc')
                    ->get()
                    ->toArray();

            }else{

                $type_list = Department::with([
                    'typelist' => function($query){
                        $query->select('id','type_name','depart_id')->orderBy('sort','desc');
                    }
                ])->where('id','>',0)
                    ->select('id','department_name')
                    ->orderBy('id','desc')
                    ->get()
                    ->toArray();
            }

            Cache::put($key,$type_list,600);
        }

        return $type_list;
    }


    public function getBookList($type_id){

        if($type_id){
            if(is_array($type_id)){

                $list = Handbook::with([
                        'type'=>function($query){
                            $query->select('id','type_name');
                        }
                    ])->where(['state'=>1])
                    ->whereIn('id',$type_id)
                    ->select('id','type_id','title','img_url','desc_con','created_at')
                    ->orderBy('sort_num','desc')
                    ->orderBy('id','desc')
                    ->paginate(5);

                $list->type_name = '全部文档';

            }else{
                $list = Handbook::where(['type_id'=>$type_id,'state'=>1])
                    ->select('id','type_id','title','img_url','desc_con','created_at')
                    ->orderBy('sort_num','desc')
                    ->orderBy('id','desc')
                    ->paginate(5);
                $list->type_name  = Type::where('id',$type_id)->value('type_name');
            }

        }else{

            $list = Handbook::with([
                    'type'=>function($query){
                        $query->select('id','type_name');
                    }
                ])->where(['state'=>1])
                ->select('id','type_id','title','img_url','desc_con','created_at')
                ->orderBy('sort_num','desc')
                ->orderBy('id','desc')
                ->paginate(5);

            $list->type_name = '全部文档';

        }


        return $list;
    }


    public function info(){
        $username = $_GET['username'];
        $id = $_GET['id'];
        $info = Handbook::where('id',$id)->first();

        $user_info = DepartmentUser::where('username',$username)->first(['id','depart_id']);
        if($user_info){

            if($user_info->depart_id == 0){//超级管理员

                $typelist = $this->getTypeList();
            }else{

                $typelist = $this->getTypeList($user_info->depart_id);
            }

            foreach ($typelist as $k=>$v){
                if(!$v['typelist']){
                    unset($typelist[$k]);
                }
            }

        }else{
            $typelist = [];
        }

        return view('web.info',['list'=>$typelist,'handbook'=>$info]);
    }


    public function handbookSearch(){
        $username = $_GET['username'];
        $text = $_GET['t_con'];

        $user_info = DepartmentUser::where('username',$username)->first(['id','depart_id']);
        if($user_info){

            if($user_info->depart_id == 0){//超级管理员

                $typelist = $this->getTypeList();
            }else{

                $typelist = $this->getTypeList($user_info->depart_id);
            }

            foreach ($typelist as $k=>$v){
                if(!$v['typelist']){
                    unset($typelist[$k]);
                }
            }

        }else{
            $typelist = $handbook = [];
        }

        $booklist = Handbook::with([
                'type'=>function($query){
                    $query->select('id','type_name');
                }
            ])->where([['state','=',1],['id','like','%'.$text.'%']])
            ->orWhere([['state','=',1],['title','like','%'.$text.'%']])
            ->select('id','type_id','title','img_url','desc_con','created_at')
            ->orderBy('sort_num','desc')
            ->orderBy('id','desc')
            ->paginate(5);

        $booklist->username = $username;
        $booklist->type_name = '搜索列表';
        $booklist->t_con = $text;
        $booklist->type_id = 0;

        return view('web.hankbook',['list'=>$typelist,'handbook'=>$booklist]);

            }

}